<template>
  <a-flex v-for="(item, index) in fvs" :key="index" style="margin-top: 8px">
    <a-input-number
      v-if="
        props.field.fieldType.T == 'FtInt' ||
        props.field.fieldType.T == 'FtFloat'
      "
      v-model:value="fvs[index]"
      style="width: (100% - 64px)"
    />
    <a-switch
      v-else-if="props.field.fieldType.T == 'FtBool'"
      v-model:checked="fvs[index]"
    ></a-switch>
    <a-date-picker
      v-else-if="props.field.fieldType.T == 'FtDate'"
      v-model:value="fvs[index]"
      valueFormat="YYYY-MM-DD"
      :locale="locale"
    ></a-date-picker>
    <a-time-picker
      v-else-if="props.field.fieldType.T == 'FtTime'"
      v-model:value="fvs[index]"
      valueFormat="HH:mm:ss"
    ></a-time-picker>
    <a-date-picker
      v-else-if="props.field.fieldType.T == 'FtDateTime'"
      v-model:value="fvs[index]"
      valueFormat="YYYY-MM-DD HH:mm:ss"
      showTime
      showNow
      :locale="locale"
    ></a-date-picker>
    <FileUploader
      ref="refFU"
      v-else-if="props.field.fieldType.T == 'FtFile'"
      :fieldDefine="field"
    >
    </FileUploader>
    <a-select
      v-else-if="props.field.fieldType.T == 'FtCode'"
      v-model:value="fvs[index]"
      :options="codeSourceMap[field.id]"
      :field-names="{ label: 'l', value: 'c' }"
      style="width: (100% - 64px)"
    >
    </a-select>
    <a-textarea
      v-else
      v-model:value="fvs[index]"
      auto-size
      style="width: (100% - 64px)"
    />
    <a-button type="link" danger @click="removeValue(index)"
      ><DeleteOutlined></DeleteOutlined
    ></a-button>
  </a-flex>
  <p>
    <a-button type="link" @click="addValue">增加值</a-button>
  </p>
</template>
<script setup>
import { ref, defineOptions } from "vue";
import "dayjs/locale/zh-cn";
import locale from "ant-design-vue/es/date-picker/locale/zh_CN";
import FileUploader from "@/component/field/FileUploader.vue";
import { DeleteOutlined } from "@ant-design/icons-vue";
import CommonDefine from "@/component/CommonDefine";

const props = defineProps({
  field: Object,
  codeSourceMap: Object,
});
const channel = CommonDefine.channel();
const fvs = ref([]);

const addValue = () => {
  fvs.value.push("");
};

const removeValue = (index) => {
  fvs.value.splice(index, 1);
};

const setValue = () => {
  fvs.value = [];
  let rec = channel.recordInEditting;
  if (rec && rec.value[props.field.id]) {
    rec.value[props.field.id].C.forEach((x) => {
      fvs.value.push(x);
    });
  }
};

const getValue = () => {
  return { fid: props.field.id, value: fvs.value };
};

const resetValue = () => {
  CommonDefine.clearArray(fvs.value);
};

// 设置 inheritAttrs 为 false
defineOptions({
  inheritAttrs: false,
});

defineExpose({ getValue, setValue, resetValue });
</script>
